#include "WebsocketServerExample.h"

WebsocketServerExample::WebsocketServerExample(mg_mgr *mgr)
{
    _websocketServer = new WWebsocketServer(mgr);
    _websocketServer->setListener(this);
}

WebsocketServerExample::~WebsocketServerExample()
{
    delete _websocketServer;
}

bool WebsocketServerExample::startWsServer(int port)
{
    return _websocketServer->startWsServer(port);
}

void WebsocketServerExample::run()
{
    _websocketServer->run();
}

void WebsocketServerExample::onWsServerReceiveMsg(mg_connection *wsConnection, shared_ptr<string> receiveMsg)
{
    WLogi() << "WebsocketServerExample::onWsReceiveMsg msg: " << *receiveMsg << ", fd: " << (long int)wsConnection->fd;
    _websocketServer->sendMsg(wsConnection, "hello, I am websockect server");
}

void WebsocketServerExample::onWsServerConnect(mg_connection *wsConnection)
{
    WLogi() << "WebsocketServerExample::onWsConnect fd: " << (long int)wsConnection->fd;
}

void WebsocketServerExample::onWsServerClosed(mg_connection *wsConnection)
{
    WLogi() << "WebsocketServerExample::onWsClosed fd: " << (long int)wsConnection->fd;
}

bool WebsocketServerExample::checkIfValidWs(mg_connection *, mg_http_message *hm)
{
    string uri(hm->uri.ptr, hm->uri.len);
    if (uri == "/api/test")
    {
        return true;
    }
    else
    {
        return false;
    }
}
